<?php
/* 
 * This file is the nerveous system of the entire framework
 * It loads the config files and dispatches the requests. to the appropriate
 * controller.
 */

/**
 * This function automagically loads the required files, according to the name.
 * This is tighly used with the dispatcher.
 * @param string $className
 */
function __autoload($className) {

    if (file_exists(LIB_PATH . DS . $className . '.class.php')) {
        require(LIB_PATH  . DS . $className . '.class.php');
    } else if (file_exists(APP_PATH . DS . 'controllers' . DS . $className . '.php')) {
        require(APP_PATH . DS . 'controllers' . DS . $className . '.php');
    } else if (file_exists(APP_PATH . DS . 'models' . DS . $className . '.php')) {
        require(APP_PATH . DS . 'models' . DS . $className . '.php');
    } else {
        throw new Exception('Class not found');
    }
}

function dispatcher() {

    //'car' stands for Controller Action and Request
    $car = Smvc_Router::route($_SERVER['REQUEST_URI']);

    //we prepare the name of the controller and the action
    $cname = ucfirst($car[0][0]);
    $controller = $cname . 'Controller';
    $action = ucfirst($car[0][1]);
    $request = explode('/',$car[1]);

    //creates an instance of the chosen controller and execute the appropriate action
    //with the rest of the resquest a parameter
    $triggeredController = new $controller($request);
    call_user_func(array($triggeredController,'execute'.$action) , $request);

    //Make all the variables of the controller directly available to the view.
    extract($triggeredController->getViewVariables());
    //make sure we get the view in the output buffer, so we can use the decorator pattern
    ob_start();
    include(APP_PATH . DS . 'views' . DS . $cname . DS . $action . '.php');
    $smvc_content = ob_get_contents();
    ob_end_clean();

    //include the view in the general layout
    include(APP_PATH . DS . 'views' . DS . 'layout.php');


}

//initialize doctrine
require_once('../vendors/Doctrine-1.2.2/lib/doctrine.php');

//DOCTRINE CONFIGURATION
spl_autoload_register(array('Doctrine_Core', 'autoload'));
spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));

$manager = Doctrine_Manager::getInstance();

$manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
$manager->setAttribute(Doctrine_Core::ATTR_EXPORT, Doctrine_Core::EXPORT_ALL);
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine_Core::ATTR_TBLNAME_FORMAT, Qp_Setup::DB_TABLE_PREFIX.'%s');

$conn = Doctrine_Manager::connection('mysql://'.Qp_Setup::DB_USER.':'.Qp_Setup::DB_PASSWORD.'@'.Qp_Setup::DB_HOST.'/'.Qp_Setup::DB_NAME);

$conn->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
$conn->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);

$conn->setCollate(Qp_Setup::DB_COLLATE);
$conn->setCharset(Qp_Setup::DB_CHARSET);

Doctrine_Core::loadModels(dirname(__FILE__).'/Base/Models');
//END DONCTRINE CONFIG


dispatcher();
